package com.core.update;

import com.data.excel.FromExcel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.DeleteRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.DeleteResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.util.Ref;

import java.io.IOException;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

public class restTestCaseDetails {
	  public static String  getTestCaseDetails(String username, String password, String testCaseId ) throws URISyntaxException, IOException {
	// Create and configure a new instance of RallyRestApi         
	RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"),username, password);
	restApi.setWsapiVersion("1.37");         
	restApi.setApplicationName("Get Test case name");     
	Map<String,String> testCaseDetails = null;
	String workspaceRef = "/workspace/QES";
	String projectRef = "/project/Stingray";

	System.out.println("Test Case ID: "+testCaseId);
	//Query User         
	QueryRequest userRequest = new QueryRequest("User");         
	userRequest.setFetch(new Fetch("UserName", "Subscription", "DisplayName"));         
	userRequest.setQueryFilter(new QueryFilter("UserName", "=", username));         
	QueryResponse userQueryResponse = restApi.query(userRequest);         
	JsonArray userQueryResults = userQueryResponse.getResults();         
	JsonElement userQueryElement = userQueryResults.get(0);         
	JsonObject userQueryObject = userQueryElement.getAsJsonObject();         
	String userRef = userQueryObject.get("_ref").toString();          
	// Query for Test Case to which we want to add results         
	QueryRequest testCaseRequest = new QueryRequest("TestCase");         
	testCaseRequest.setFetch(new Fetch("FormattedID","Name"));         
	testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", testCaseId));   
	testCaseRequest.setWorkspace(workspaceRef);
	//testCaseRequest.setProject(projectRef);
	QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);         
	JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();         
	String testCaseName = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("Name").toString();
	System.out.println("Test Case Name: "+ testCaseName);
	
	//testCaseDetails.put("testCaseName", testCaseName);
	return testCaseName;
	  }
	
	}

